/**
 * 后台js工具类
 */
let jsUtils = {
  getFormatMoney: function (account) {
    let ret = '';
    if (account != null) {
      if (account >= 10000) {
        ret = account / 10000;
        ret = formatMoney(ret, 1) + ' 万元';
      } else {
        ret = formatMoney(account, 1) + ' 元';
      }
    }
    return ret;
  },

  getTerm: function (proTerm) {
    let ret = '';
    if (proTerm != null) {
      if (proTerm > 360) {
        ret = proTerm / 360 + ' 年';
      } else if (proTerm > 30) {
        ret = proTerm / 30 + ' 月';
      } else {
        ret = proTerm + ' 天';
      }
    }
    return ret;
  },
  serializeJson: function (jq) {
    let arr = jq.serializeArray();
    let ret = {};
    if (arr) {
      for (let i = 0; i < arr.length; i++) {
        let k = arr[i].name;
        let v = arr[i].value;
        if (typeof ret[k] !== 'undefined') {
          if (ret[k] instanceof Array) {
            ret[k].push(v);
          } else {
            ret[k] = [ret[k], v];
          }
        } else {
          ret[k] = v;
        }
      }
    }
    return ret;
  },

    /**
     * 转换日期对象为日期字符串
     *
     * @param date
     *            日期对象
     * @param isFull
     *            是否为完整的日期数据, 为true时, 格式如"2000-03-05 01:05:04" 为false时, 格式如
     *            "2000-03-05"
     * @return 符合要求的日期字符串
     */
  getSmpFormatDate: function (l, isFull) {
    let date = new Date(l);
    let pattern = '';
    if (isFull == true || isFull == undefined) {
      pattern = 'yyyy-MM-dd hh:mm:ss';
    } else {
      pattern = 'yyyy-MM-dd';
    }
    return date.format(pattern);
  },

    /**
     * "2017-08-06T16:00:00.000Z"
     */
  getFormatDate: function (beginDate) {
    console.log(beginDate);
    if (!beginDate) {
      return
    }
    console.log('time = ' + beginDate);
    let beginDateStr = beginDate.toLocaleString().substr(0, 10);


    let time = beginDateStr.split('/');

    let year = time[0];
    let month = time[1];
    let day = time[2];

    if (month.toString().length == 1) {
      month = '0' + month;
    }

    if (day.toString().length == 1) {
      day = '0' + day;
    }

    let result = year + '-' + month + '-' + day;
    console.log('result = ' + result);
    return result;
  },
  /**
   * 时间前后的对比
   */
  compTime (beginTime, endTime) { //开始时间结束时间对比
    let beginTimes = beginTime.substring(0, 10).split('-');
    let endTimes = endTime.substring(0, 10).split('-');
    beginTime = beginTimes[1] + '-' + beginTimes[2] + '-' + beginTimes[0] + ' ' + beginTime.substring(10, 19);
    endTime = endTimes[1] + '-' + endTimes[2] + '-' + endTimes[0] + ' ' + endTime.substring(10, 19);
    let a = (Date.parse(endTime) - Date.parse(beginTime)) / 3600 / 1000;
    if (a < 0) {
      return 'less';
    } else if (a > 0) {
      return 'exception'
    } else if (a === 0) {
      return 'equal';
    }
  }
}

Date.prototype.format = function (format) //
    {
  let o = {
    'M+': this.getMonth() + 1, // month
    'd+': this.getDate(), // day
    'h+': this.getHours(), // hour
    'm+': this.getMinutes(), // minute
    's+': this.getSeconds(), // second
    'q+': Math.floor((this.getMonth() + 3) / 3), // quarter
    'S': this.getMilliseconds()
                // millisecond
  };
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear() + '')
                .substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
                    : ('00' + o[k]).substr(('' + o[k]).length));
    }
  }
  return format;
}

export {
    jsUtils
}

export default { //很关键
  jsUtils
}
